package cn.kjcoder.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hkj
 * @email 1349091387@qq.com
 * @date 2022/1/21
 * 源码允许修改，并且本人十分乐意有 大佬 来对代码进行 随 意 调 教
 **/
@RestController
@Slf4j
@RequestMapping("/random")
public class RandomController {

    @Autowired
    private RedisTemplate redisTemplate;

    public static final String PRIZE_KEY = "jd:prize";

    @GetMapping("/prize")
    public String prize(){
        String result = "";
        try {
            //随机获取一个序列化
            String num  = (String) this.redisTemplate.opsForSet().randomMember(PRIZE_KEY);
            if(!StringUtils.isEmpty(num)){
                //切割，得到前缀
                int no = Integer.parseInt(num.split("-")[0]);
                log.info(String.valueOf(no));
                switch (no){
                    case 0:
                        result = "谢谢参与";
                        break;
                    case 1:
                        result = "获得1个觅币";
                        break;
                    case 5:
                        result = "获得5个觅币";
                        break;
                    case 10:
                        result = "获得10个觅币";
                        break;
                    default:
                        result = "谢谢参与";
                }
            }
            log.info("查询的结果：{}",num);
        } catch (NumberFormatException e) {
            log.info("exception:" + e);
        }
        return result;
    }
}
